<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button
      type="primary"
      plain
      @click="openForm('create')"
      v-hasPermi="['erp:base-machine:create']"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键ID" align="center" prop="id" />
       <el-table-column label="设备类型" align="center" prop="machineType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MACHINE_TYPE" :value="scope.row.machineType" />
        </template>
      </el-table-column>
      <el-table-column label="串口" align="center" prop="serialPort">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SERIAL_PORT" :value="scope.row.serialPort" />
        </template>
      </el-table-column>
      <el-table-column label="波特率" align="center" prop="baudRate">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BAUD_RATE" :value="scope.row.baudRate" />
        </template>
      </el-table-column>
      <el-table-column label="数据位" align="center" prop="dataBits">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DATA_BITS" :value="scope.row.dataBits" />
        </template>
      </el-table-column>
      <el-table-column label="停止位" align="center" prop="stopBits">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.STOP_BITS" :value="scope.row.stopBits" />
        </template>
      </el-table-column>
      <el-table-column label="校验位" align="center" prop="parity">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PARITY" :value="scope.row.parity" />
        </template>
      </el-table-column>
      <el-table-column label="发送命令" align="center" prop="sendCommand" />
      <el-table-column label="接收命令" align="center" prop="receiveCommand" />
      <el-table-column label="设备连接类型" align="center" prop="connectionType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONNECTION_TYPE" :value="scope.row.connectionType" />
        </template>
      </el-table-column>
      <el-table-column label="本机端口" align="center" prop="localPort" />
      <el-table-column label="TPCIP" align="center" prop="tcpIp" />
      <el-table-column label="目标端口" align="center" prop="targetPort" />
      <el-table-column label="登录账号" align="center" prop="loginAccount" />
      <el-table-column label="登录密码" align="center" prop="loginPassword" />
      <el-table-column label="数据通道" align="center" prop="dataChannel" />
      <el-table-column label="数据保存路径" align="center" prop="dataSavePath" />
      <el-table-column label="码流类型" align="center" prop="streamType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.STREAM_TYPE" :value="scope.row.streamType" />
        </template>
      </el-table-column>
      <el-table-column label="是否启用 (0-否, 1-是)" align="center" prop="enabled">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.enabled" />
        </template>
      </el-table-column>
      <el-table-column label="数据长度" align="center" prop="dataLength" />
      <el-table-column label="初始状态数据" align="center" prop="initialStateData" />
      <el-table-column label="数据开始索引" align="center" prop="dataStartIndex" />
      <el-table-column label="数据结束索引" align="center" prop="dataEndIndex" />
      <el-table-column label="数据结束标识字符" align="center" prop="dataEndMarker" />
      <el-table-column label="最小数量" align="center" prop="minQuantity" />
      <el-table-column label="稳定时间" align="center" prop="stableTime" />
      <el-table-column label="是否逆序 (0-否, 1-是)" align="center" prop="reverseOrder">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.reverseOrder" />
        </template>
      </el-table-column>
      <el-table-column label="数据结果处理" align="center" prop="dataResultProcessing" />
      <el-table-column label="读取数据间隔" align="center" prop="readInterval" />
      <el-table-column label="编号" align="center" prop="number" />
      <el-table-column label="对接协议方案" align="center" prop="protocolScheme">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PROTOCOL_SCHEME" :value="scope.row.protocolScheme" />
        </template>
      </el-table-column>
      <el-table-column label="发送目录" align="center" prop="sendDirectory" />
      <el-table-column label="工序ID" align="center" prop="processId" />
      <el-table-column label="工序名称" align="center" prop="processName" />
      <el-table-column label="站号" align="center" prop="stationNumber" />
      <el-table-column label="地址是否从0开始 (0-否, 1-是)" align="center" prop="addressStartFromZero">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.addressStartFromZero" />
        </template>
      </el-table-column>
      <el-table-column label="数据解析格式" align="center" prop="dataParseFormat">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DATA_PARSE_FORMAT" :value="scope.row.dataParseFormat" />
        </template>
      </el-table-column>
      <el-table-column label="连接服务器" align="center" prop="serverConnection" />
      <el-table-column label="数据库名称" align="center" prop="databaseName" />
      <el-table-column label="数据库账号" align="center" prop="databaseAccount" />
      <el-table-column label="数据库密码" align="center" prop="databasePassword" />
      <el-table-column label="创建ERP单据 (0-否, 1-是)" align="center" prop="createErpDocument">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.createErpDocument" />
        </template>
      </el-table-column>
      <el-table-column label="系列" align="center" prop="series" />
      <el-table-column label="LocalTSAP" align="center" prop="localTsap" />
      <el-table-column label="DestTSAP" align="center" prop="destTsap" />
      <el-table-column label="发送URL" align="center" prop="sendUrl" />
      <el-table-column label="操作间隔" align="center" prop="operationInterval" />
      <el-table-column label="是否启用站号信息 (0-否, 1-是)" align="center" prop="enableStationInfo">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.enableStationInfo" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:base-machine:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:base-machine:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
    <!-- 表单弹窗：添加/修改 -->
    <BaseMachineParameterForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { BaseMachineApi } from '@/api/erp/basemachine'
import BaseMachineParameterForm from './BaseMachineParameterForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  machineId?: number // 机台id（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  machineId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.machineId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.machineId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BaseMachineApi.getBaseMachineParameterPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.machineId) {
    message.error('请选择一个ERP基础资料-生产机器')
    return
  }
  formRef.value.open(type, id, props.machineId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BaseMachineApi.deleteBaseMachineParameter(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>
